App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider',
function ($stateProvider, $locationProvider, $urlRouterProvider) {
  'use strict';

  $locationProvider.html5Mode(false);

  // defaults to login
  $urlRouterProvider.otherwise('/page/login');

  // 
  // Application Routes
  // -----------------------------------   
  $stateProvider
    .state('app', {
        url: '/app',
        abstract: true,
        templateUrl: 'pages/app.html',
    })
	.state('app.main', {
        url: '/main',
        title: '首页',
        templateUrl: 'views/main.html'
    })
    .state('app.test1', {
        url: '/test1',
        title: 'test1',
        templateUrl: 'views/test/test1.html'
    })
    .state('app.test2', {
        url: '/test2',
        title: 'test2',
        templateUrl: 'views/test/test2.html'
    })
    
    // 
    // Single Page Routes
    // ----------------------------------- 
    .state('page', {
        url: '/page',
        templateUrl: 'pages/page.html',
    })
    .state('page.login', {
        url: '/login',
        title: "Login",
        templateUrl: 'pages/login.html'
    })
    .state('page.404', {
        url: '/404',
        title: "404",
        templateUrl: 'pages/404.html'
    })
    .state('page.500', {
        url: '/500',
        title: "500",
        templateUrl: 'pages/500.html'
    })
    ;

}]).config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
    function ( $controllerProvider, $compileProvider, $filterProvider, $provide) {
      'use strict';
      // registering components after bootstrap
      App.controller = $controllerProvider.register;
      App.directive  = $compileProvider.directive;
      App.filter     = $filterProvider.register;
      App.factory    = $provide.factory;
      App.service    = $provide.service;
      App.constant   = $provide.constant;
      App.value      = $provide.value;
}]).factory('httpInterceptor', [ '$log','$q', '$injector',function($log, $q, $injector) {

    var httpInterceptor = {
        'responseError' : function(response) {

            var rootScope = $injector.get('$rootScope');
            if (response.status === 518) {

                rootScope.$state.go("page.login");
                return $q.reject(response);
            } else if (response.status === 401) {

                rootScope.$state.go("page.404");
                return $q.reject(response);
            } else if (response.status === 404) {
            	
            	rootScope.$state.go("page.404");
                return $q.reject(response);
            } else {

            	rootScope.$state.go("page.500");
                return $q.reject(response);
            }
        }
    };
    return httpInterceptor;
}
]).config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
}]);